﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Common;
using V5_Model;
using V5_BLL;

namespace V5_EShop.Admin
{
    public partial class Admin_Admin : BasicPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Action = Input.SetString(Request["Action"]);
                switch (Action.Trim().ToLower())
                {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        this.Bind_gvList();
                        break;
                }
            }
        }

        private void Bind_gvList()
        {
            this.MultiViewList.ActiveViewIndex = 0;
            this.gvList.DataSource = new BLLAdmin().GetAllList();
            this.gvList.DataBind();
        }

        private void Bind_DataEdit(int id)
        {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelAdmin model = new BLLAdmin().GetModel(id);
            if (model != null)
            {
                this.txtAdminName.Text = model.AdminName;
                this.lblTitle.Text = "修改管理员";
                this.btnSave.Text = "保存修改";
                this.hfID.Value = model.ID.ToString();
            }
        }

        private void Bind_Delete(int id)
        {
            new BLLAdmin().Delete(id);
            this.Bind_gvList();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ModelAdmin model = new ModelAdmin();
            BLLAdmin bll = new BLLAdmin();
            int id = Input.SetInt(this.hfID.Value);
            string AdminName = this.txtAdminName.Text;
            string AdminPwd = this.txtAdminPwd.Text;
            string AdminComPwd = this.txtAdminComPwd.Text;
            if (AdminName.Trim() == "")
            {
                Js.Alert("用户名不能为空!");
                return;
            }
            if (AdminPwd.Trim() == "")
            {
                Js.Alert("密码不能为空!");
                return;
            }
            if (AdminComPwd.Trim() == "")
            {
                Js.Alert("确认密码不能为空!");
                return;
            }
            if (AdminPwd.Trim() != AdminComPwd.Trim())
            {
                Js.Alert("两次输入的密码不相同!");
                return;
            }
            AdminPwd = Input.MD5(AdminPwd, 16);
            model.AdminName = AdminName;
            model.AdminPwd = AdminPwd;
            model.LoginTime = DateTime.Now;
            if (id == 0)
            {
                model.ID = bll.GetMaxId();
                bll.Add(model);
            }
            else if (id > 0)
            {
                model.ID = id;
                bll.Update(model);
            }
            this.Bind_gvList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string eName = Input.SetString(e.CommandName);
            int id = Input.SetInt(e.CommandArgument);
            switch (eName.ToLower().Trim())
            {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
            }
        }
    }
}
